package cn.bdqn.layui.controller;

import cn.bdqn.layui.entity.Bar;
import cn.bdqn.layui.entity.User;
import cn.bdqn.layui.entity.vo.UserVO;
import cn.bdqn.layui.service.menu.MenuService;
import cn.bdqn.layui.service.user.UserService;
import cn.bdqn.layui.util.ResponseResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    UserService userService;

    @Resource
    MenuService menuService;


    @RequestMapping({"/index", "/"})
    public String idnex(Model model) {
        List<Bar> barList = menuService.findMenuRoleAll();
        model.addAttribute("barList", barList);
        return "/views/index";
    }


    /**
     * 登录
     * @return
     */
   /* @RequestMapping("/dologin")
    @ResponseBody
    public ResponseResult doLogin() {
        String access_token = "2121212152";
        return new ResponseResult(0, "登陆成功", access_token);
    }*/


    /**
     * 根据条件分页查询
     *
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @GetMapping("/userlist")
    @ResponseBody
    public String userList(Integer page, Integer limit, Integer id, String username) {
        if (username == null || username.equals("")) {
            username = null;
        }
        id = id == null ? 0 : id;

        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        List<User> userList = userService.findUserList((page - 1) * limit, limit, id, username);
        int count = userService.findUserCount(id, username);
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "");
        object.put("data", userList);
        object.put("count", count);
        return object.toJSONString();
    }


    /**
     * 添加数据
     *
     * @return
     */
    @RequestMapping("/adduser")
    @ResponseBody
    public ResponseResult addCollector(UserVO userVO) {
        int i = userService.addUser(userVO);
        if (i > 0) {
            userService.addUserRole(userVO.getId(), 2);
        }
        return new ResponseResult(200, "", i);
    }

    /**
     * 登录
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUser")
    public ResponseResult delUser(String id) {
        //int num = 0;
       /* String[] ids = id.split(",");
        for (String Uid : ids) {
            num = userService.delUser(Uid);
        }
        if (num > 0) {
            return new ResponseResult(0, "");
        }*/
        int i = userService.delUser(id);
        if (i > 0) {
            List<UserVO> byUid = userService.findByUid(Integer.parseInt(id));
            for (UserVO user : byUid) {
                userService.deleteUserRole(user.getUid());
            }
        }
        return new ResponseResult(0, "");
    }

    @RequestMapping("/updateroleuser")
    @ResponseBody
    public ResponseResult updateroleuser(UserVO userVO) {
        /*User user = userService.findByUserName(userVO.getUsername(),-1);
        if(user==null && userVO.getRoles().length>0){*/
        userService.updateUser(userVO);
           /* user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
            int i = userService.updateUser(userVO);
        }*/
        return new ResponseResult(200, "", 0);
    }
}
